package com.zhiyin.seal.controller;

import com.zhiyin.seal.anno.OperateLog;
import com.zhiyin.seal.constant.ResponseCodeConst;
import com.zhiyin.seal.entity.bo.SystemLogBo;
import com.zhiyin.seal.entity.bo.SystemRoleBo;
import com.zhiyin.seal.entity.bo.SystemUserBo;
import com.zhiyin.seal.entity.dto.ResponseDTO;
import com.zhiyin.seal.entity.dto.SystemRoleDTO;
import com.zhiyin.seal.entity.dto.SystemUserDTO;
import com.zhiyin.seal.entity.vo.*;
import com.zhiyin.seal.service.*;
import com.zhiyin.seal.util.BeanUtil;
import com.zhiyin.seal.util.StringUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 管理后台控制器
 */
@RestController
@RequestMapping("/admin-web")
public class AdminController {

    @Autowired
    AdminWebService adminWebService;

    @Autowired
    CompanyService companyService;

    @Autowired
    UserService userService;

    @Autowired
    SealService sealService;

    @Autowired
    StampService stampService;

    /**
     * 用户登录
     * @param userDTO
     * @return
     */
    @PostMapping("/user/login")
    public ResponseDTO<SystemUserVo> login(@RequestBody SystemUserDTO userDTO, HttpServletResponse response){
        String password = userDTO.getPassword();
        String md5Pwd = DigestUtils.md5Hex(password).toUpperCase();
        SystemUserBo userBo = adminWebService.login(userDTO.getUserName(),md5Pwd);
        if(userBo != null){
            SystemUserVo userVo = BeanUtil.copy(userBo,SystemUserVo.class);
            String accessToken = StringUtil.generateToken(userBo.getId().toString(),userBo.getUserName());
            response.setHeader("x-access-token",accessToken);

            // 记录日志
            SystemLogBo systemLogBo = new SystemLogBo();
            systemLogBo.setOperateUser(userBo.getUserName());
            systemLogBo.setOperateType("用户登录");
            adminWebService.addSystemLog(systemLogBo);

            return ResponseDTO.succData(userVo);
        }

        return ResponseDTO.wrap(ResponseCodeConst.NOT_EXISTS);
    }

    /**
     * 获取系统用户列表
     * @return
     */
    @GetMapping("/user/list")
    public ResponseDTO<List<SystemUserVo>> getSystemUserList(){
        return ResponseDTO.succData(BeanUtil.copyList(adminWebService.getSystemUserList(),SystemUserVo.class));
    }

    /**
     * 添加后台用户
     * @param userDTO
     * @return
     */
    @OperateLog(description = "添加后台用户")
    @PostMapping("/user/add")
    public ResponseDTO<Boolean> addSystemUser(@RequestBody SystemUserDTO userDTO){
        SystemUserBo systemUserBo = adminWebService.getSystemUserByName(userDTO.getUserName());
        if(systemUserBo != null){
            return ResponseDTO.wrap(ResponseCodeConst.DUPLICATE_KEY_ERROR);
        }

        systemUserBo = BeanUtil.copy(userDTO,SystemUserBo.class);
        String md5Pwd = DigestUtils.md5Hex(userDTO.getPassword()).toUpperCase();
        systemUserBo.setUserPwd(md5Pwd);

        return ResponseDTO.succData(adminWebService.addSystemUser(systemUserBo));
    }

    /**
     * 修改后台用户密码
     * @param userDTO
     * @return
     */
    @OperateLog(description = "修改后台用户密码")
    @PostMapping("/user/modify/password")
    public ResponseDTO<Boolean> modifyUserPassword(@RequestBody SystemUserDTO userDTO){
        String md5Pwd = DigestUtils.md5Hex(userDTO.getNewPassword()).toUpperCase();
        return ResponseDTO.succData(adminWebService.modifyUserPassword(userDTO.getId(),md5Pwd));
    }

    /**
     * 变更用户角色
     * @param userDTO
     * @return
     */
    @OperateLog(description = "变更用户角色")
    @PostMapping("/user/role/change")
    public ResponseDTO<Boolean> changeUserRole(@RequestBody SystemUserDTO userDTO){
        return ResponseDTO.succData(adminWebService.changeUserRole(userDTO.getId(),userDTO.getSystemRoleId()));
    }

    /**
     * 删除后台用户
     * @param userDTO
     * @return
     */
    @OperateLog(description = "删除后台用户")
    @PostMapping("/user/delete")
    public ResponseDTO<Boolean> deleteUser(@RequestBody SystemUserDTO userDTO){
        return ResponseDTO.succData(adminWebService.deleteUser(userDTO.getId()));
    }

    /**
     * 获取系统角色列表
     * @return
     */
    @GetMapping("/role/list")
    public ResponseDTO<List<SystemRoleVo>> getSystemRoleList(){
        return ResponseDTO.succData(BeanUtil.copyList(adminWebService.getRoleList(),SystemRoleVo.class));
    }

    /**
     * 添加系统角色
     * @param roleDTO
     * @return
     */
    @OperateLog(description = "添加系统角色")
    @PostMapping("/role/add")
    public ResponseDTO<Boolean> addRole(@RequestBody SystemRoleDTO roleDTO){
        return ResponseDTO.succData(adminWebService.addRole(BeanUtil.copy(roleDTO, SystemRoleBo.class)));
    }

    /**
     * 更新角色菜单权限
     * @param roleDTO
     * @return
     */
    @OperateLog(description = "更新角色菜单权限")
    @PostMapping("/role/right/update")
    public ResponseDTO<Boolean> updateRoleRight(@RequestBody SystemRoleDTO roleDTO){
        return ResponseDTO.succData(adminWebService.updateRoleRight(roleDTO.getId(),roleDTO.getMenuIdList()));
    }

    /**
     * 获取角色菜单权限
     * @param roleId
     * @return
     */
    @GetMapping("/role/right/ids")
    public ResponseDTO<List<Long>> getUserRightIdList(@RequestParam Long roleId){
        return ResponseDTO.succData(adminWebService.getRoleRightIdList(roleId));
    }

    /**
     * 删除系统角色
     * @param roleDTO
     * @return
     */
    @OperateLog(description = "删除系统角色")
    @PostMapping("/role/delete")
    public ResponseDTO<Boolean> deleteRole(@RequestBody SystemRoleDTO roleDTO){
        return ResponseDTO.succData(adminWebService.deleteRole(roleDTO.getId()));
    }


    @GetMapping("/dashboard")
    public ResponseDTO<DashboardDataVo> getDashboardData(){
        DashboardDataVo dashboardDataVo = new DashboardDataVo();
        dashboardDataVo.setCompanyCount(companyService.getCompanyCount());
        dashboardDataVo.setUserCount(userService.getUserCount());
        dashboardDataVo.setSealCount(sealService.getSealCount());
        dashboardDataVo.setStampCount(stampService.selectStampCount());

        return ResponseDTO.succData(dashboardDataVo);
    }

    /**
     * 获取服务器运行状态
     * @return
     */
    @GetMapping("/server/status")
    public ResponseDTO<ServerStatusVo> getServerStatus(){
        return ResponseDTO.succData(BeanUtil.copy(adminWebService.getServerStatus(),ServerStatusVo.class));
    }

    /**
     * 获取系统日志
     * @return
     */
    @GetMapping("/system/log")
    public ResponseDTO<List<SystemLogVo>> getSystemLogList(){
        return ResponseDTO.succData(BeanUtil.copyList(adminWebService.getSystemLogList(),SystemLogVo.class));
    }
}
